package com.volkanic.views.menu.stages;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.volkanic.controller.BackController;
import com.volkanic.controller.GameController;
import com.volkanic.controller.menu.MenuController;
import com.volkanic.model.buttons.MenuButton;
import com.volkanic.model.buttons.MotionTextButton;
import com.volkanic.views.stages.MainStage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Lucas on 22/07/2014.
 */
public class ButtonStage extends MainStage {

    private final String TAG = "ButtonStage";
    /**
     * The list of button of the ButtonStage
     */
    private List<Button> _buttonsList;
    /**
     * The controller as the inputProcessor for the back button
     */
    protected BackController _backController;
    /**
     * The InputMultiplexer to handle the different InputProcessor
     */
    protected InputMultiplexer _inputMultiplexer;

    /**
     * Create a new instance of the class ButtonStage
     * @param controller The controller of the game
     */
    public ButtonStage(GameController controller){
        this.setupProcessor(controller);
        this.setupButton(controller);
        this.addButtonsToStage();
    }

    /**
     * Setup the different processor
     * @param controller
     */
    private void setupProcessor(GameController controller){
        // set the input processors on this stage
        _inputMultiplexer = new InputMultiplexer();
        _backController = new BackController(controller);

        _inputMultiplexer.addProcessor(_backController);
        _inputMultiplexer.addProcessor(this);

        Gdx.input.setInputProcessor(_inputMultiplexer);
    }

    /**
     * Create a series of button to show on the display
     * @param controller the game controller
     */
    private void setupButton(GameController controller) {
        // Create the list of buttons
        _buttonsList = Collections.synchronizedList(new ArrayList<Button>());

        // Create the button, set the InputListener and fill the list of MenuButton
        MotionTextButton play = CreateMenuButton("Play", 21f , 2000f, 500f);
        play.addListener(new MenuController(controller, GameController.StageState.WORLD_SELECTION));

        MotionTextButton highscore = CreateMenuButton("HighScore", 20f, 1700f, 360f);
        highscore.addListener(new MenuController(controller, GameController.StageState.HIGHSCORE));

        MotionTextButton multi = CreateMenuButton("Multi-player", 19f, 1450f, 240f);
        multi.addListener(new MenuController(controller, GameController.StageState.MULTIPALYER));

        MotionTextButton exit = CreateMenuButton("Exit", 18f, 1200f, 120f);
        exit.addListener(new MenuController(controller, GameController.StageState.EXIT));

        synchronized (_buttonsList){
            _buttonsList.add(play);
            _buttonsList.add(highscore);
            _buttonsList.add(multi);
            _buttonsList.add(exit);
        }
    }

    /**
     * Create a new motion text button
     * @param text the text to write in the button
     * @param speed the speed of the motion
     * @param curY The Y coordinate where the motion start
     * @param endY The Y coordinate where the motion end
     * @return a new motion text button
     */
    private MotionTextButton CreateMenuButton(String text, float speed, float curY, float endY){
        Gdx.app.debug(TAG, "Creating button Menu");
        Vector2 origin = new Vector2(X_CENTER, curY * HEIGHT_UNIT);
        Vector2 end = new Vector2(X_CENTER, endY * HEIGHT_UNIT);
        return MenuButton.CreateMenuButton(text, speed * HEIGHT_UNIT, origin, end);
    }

    /**
     * Add each button of the list into the stage
     */
    private void addButtonsToStage() {
        synchronized (_buttonsList){
            Iterator<Button> iteratorButton = _buttonsList.iterator();
            while(iteratorButton.hasNext()) {
                this.addActor(iteratorButton.next());
            }
        }
    }

    @Override
    public void dispose() {
        super.dispose();
        Gdx.app.debug(TAG, "disposing ButtonScreen");

        _inputMultiplexer.clear();
    }
}
